package com.bolong.gmgc;

import java.util.Map;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 极光推送工具类
 * 
 * @author hainan0421@163.com
 * @date 2017年7月5日 下午5:23:16
 */
public class JPushUtils {

	private static String appKey = "9f03c7916099d7cd59308bc2";
	private static String masterSecret = "71e564e38d57c0e55024490b";

	private static final String pdaKey = "b5a3fb241be206c13488f454";
	private static final String pdaMasterSecret = "78ccbf61e1d9334cee66c9a8";

	public static void setAppKey(Integer emu) {
		if (emu == 1) {
			appKey = "9f03c7916099d7cd59308bc2";
			masterSecret = "71e564e38d57c0e55024490b";
		}
		if (emu == 2) {
			appKey = pdaKey;
			masterSecret = pdaMasterSecret;
		}
	}

	// 残缺alias推送
	public static void sendPushByAliasAloneAlert(String alias, String alert) {
		// 链接对象
		JPushClient jpushClient = new JPushClient(pdaMasterSecret, pdaKey, null, ClientConfig.getInstance());

		// 推送
		PushPayload payload = PushPayload.newBuilder() // 构建
				.setAudience(Audience.alias(alias)) // 别名
				.setPlatform(Platform.all()) // 所有设备
				.setOptions(Options.newBuilder().setApnsProduction(true).build()) // 生产环境
				.setNotification(Notification.alert(alert)) // 推送内容
				.build();
		try {
			// 执行
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			System.out.println("Should review the error, and fix the request" + e);
			System.out.println("HTTP Status: " + e.getStatus());
			System.out.println("Error Code: " + e.getErrorCode());
			System.out.println("Error Message: " + e.getErrorMessage());
		}
	}

	// 完整alias推送
	public static void sendPushByAlias(String alias, String title, String content, Map<String, String> map) {

		// 链接对象
		JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());

		// 推送内容
		PushPayload payload = PushPayload.newBuilder().setAudience(Audience.alias(alias)).setPlatform(Platform.all()) // 所有设备
				/*
				 * .setNotification(Notification.alert(content)) .build();
				 */
				.setNotification(Notification.newBuilder() // 构建通知对象
						.addPlatformNotification(AndroidNotification.newBuilder() // 安卓通知
								.addExtras(map) // 透传数据
								.setAlert(content) // 内容
								.setTitle(title) // 标题
								.build())
						.addPlatformNotification(IosNotification.newBuilder() // ios通知
								.addExtras(map) // 透传数据
								.setAlert(IosAlert.newBuilder().setTitleAndBody(title, "subtitle", content) // iosAlert构建
										.build()) // 标题 内容
								.build())
						.build())
				.setOptions(Options.newBuilder().setApnsProduction(true).build()) // 生产环境
				.build(); // 透传数据

		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			System.out.println("Should review the error, and fix the request" + e);
			System.out.println("HTTP Status: " + e.getStatus());
			System.out.println("Error Code: " + e.getErrorCode());
			System.out.println("Error Message: " + e.getErrorMessage());
		}
	}

	/**
	 * 所有平台，所有设备，内容为 【bolong发送过来的!】 的通知
	 * 
	 */
	public static PushPayload buildPushObject_all_all_alert(String alert) {
		return PushPayload.alertAll(alert);
	}

	/**
	 * 所有平台，推送目标是别名为 "alias1"，通知内容为 【神马都是浮云!】
	 * 
	 */
	public static PushPayload buildPushObject_all_alias_alert() {
		return PushPayload.newBuilder() //
				.setPlatform(Platform.android()) //
				.setAudience(Audience.alias("alias1")) //
				.setNotification(Notification.alert("神马都是浮云!")).build();
	}

	/**
	 * 平台是 Android，目标是 tag 为 "tag1" 的设备，内容是 【这是内容】，并且标题为 【这是标题】。
	 * 
	 */
	public static PushPayload buildPushObject_android_tag_alertWithTitle() {
		return PushPayload.newBuilder().setPlatform(Platform.android()) //
				.setAudience(Audience.tag("tag1")) //
				.setNotification(Notification.android("这是内容", "这是标题", null)) //
				.build();
	}

	// 测试主方法
	public static void main(String arg[]) {

		/*
		 * HashMap<String, String> map = new HashMap<>(); map.put("status", "1"); map.put("姓名", "张三"); sendPushByAlias("C62AED409A63443496D5057CEF35A7EA", "标题",
		 * "您已开始停车", map);
		 */

		JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance()); // For
		// PushPayload object.
		PushPayload payload = buildPushObject_all_all_alert("您已进入车场");
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			System.out.println("Should review the error, and fix the request" + e);
			System.out.println("HTTP Status: " + e.getStatus());
			System.out.println("Error Code: " + e.getErrorCode());
			System.out.println("Error Message: " + e.getErrorMessage());
		}

	}

}
